package com.cyms.modules.sys.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cyms.common.utils.DateUtils;
import com.cyms.common.utils.R;
import com.cyms.modules.base.entity.CarEntity;
import com.cyms.modules.base.service.CarService;
import com.cyms.modules.sys.service.SysHomeService;

@RestController
@RequestMapping("/sys/home")
public class SysHomeController extends AbstractController  {
	@Autowired
	private SysHomeService sysHomeService;
	@Autowired
    private CarService carService;
	/**
	 * 统计订单和派车数量查询列表
	 */
	@RequestMapping("/orderAndCarCountQuery")
	public R orderAndCarCountQuery(@RequestParam Map<String, Object> params) {
		params.put("createOrg",getUser().getCompanyId());
		Map<String, Object> map = sysHomeService.queryOrderAndCarCount(params);

		return R.ok().put("map", map);
	}
	
	/**
	 * 统计订单和派车数量查询列表
	 */
	@RequestMapping("/etcAndfuelAmountQuery")
	public R etcAndfuelAmountQuery(@RequestParam Map<String, Object> params) {
		params.put("createOrg",getUser().getCompanyId());
		Map<String, Object> map = sysHomeService.queryEtcAndfuelAmountQuery(params);

		return R.ok().put("map", map);
	}
	/**
	 * 统计车辆派遣和收入列表
	 */
	@RequestMapping("/carIncomeQuery")
	public R carIncomeQuery(@RequestParam Map<String, Object> params) {
		params.put("createOrg",getUser().getCompanyId());
		List<Map<String, Object>> list = sysHomeService.queryCarIncomeQuery(params);

		return R.ok().put("list", list);
	}
	
	/**
	 * 统计线路订单量列表
	 */
	@RequestMapping("/lineOrderCountQuery")
	public R lineOrderCountQuery(@RequestParam Map<String, Object> params) {
		params.put("createOrg",getUser().getCompanyId());
		List<Map<String, Object>> list = sysHomeService.queryLineOrderCountQuery(params);

		return R.ok().put("list", list);
	}
	
	/**
	 * 统计线路订单量列表
	 */
	@RequestMapping("/companyIncomeQuery")
	public R companyIncomeQuery(@RequestParam Map<String, Object> params) {
		params.put("createOrg",getUser().getCompanyId());
		List<Map<String, Object>> list = sysHomeService.queryCompanyIncomeQuery(params);

		return R.ok().put("list", list);
	}
	/**
	 * 统计ETC和加油卡充值消费记录
	 */
	@RequestMapping("/etcAndFuelRechargePayQuery")
	public R etcAndFuelRechargePayQuery(@RequestParam Map<String, Object> params) {
		params.put("createOrg",getUser().getCompanyId());
		List<Map<String, Object>> list = sysHomeService.queryEtcAndFuelRechargePayQuery(params);
		
		return R.ok().put("list", list);
	}
	@RequestMapping({"/queryCarState"})
    public R queryCarState(@RequestParam Map<String, Object> params) {
        params.put("createOrg", this.getUser().getCompanyId());
        List<Map<String, Object>> list = this.sysHomeService.queryCarState(params);
        List<CarEntity> carList = this.carService.selectList((new EntityWrapper()).eq(this.getUser().getCompanyId() != null, "create_org", this.getUser().getCompanyId()).where("company_id in (select id from cy_company where type = 0)", new Object[]{""}));
        Date startDate = DateUtils.stringToDate((String)params.get("startDate"), "yyyy-MM-dd");
        Date endDate = DateUtils.stringToDate((String)params.get("endDate"), "yyyy-MM-dd");
        List<Date> dateList = new ArrayList();
        List<String> dateStrList = new ArrayList();

        for(int i = 0; DateUtils.addDateDays(startDate, i).compareTo(endDate) <= 0; ++i) {
            dateList.add(DateUtils.addDateDays(startDate, i));
            dateStrList.add(DateUtils.format(DateUtils.addDateDays(startDate, i)));
        }

        List<List<Integer>> dataList = new ArrayList();

        for(int i = 0; i < dateList.size(); ++i) {
            for(int j = 0; j < carList.size(); ++j) {
                List<Integer> tempList = new ArrayList();
                CarEntity curCar = (CarEntity)carList.get(j);
                Date curDate = (Date)dateList.get(i);
                Long res = list.parallelStream().filter((c) -> {
                    Long carId = (Long)c.get("car_id");
                    Date sDate = (Date)c.get("start_date");
                    Date eDate = (Date)c.get("end_date");
                    return carId == curCar.getId() && curDate.compareTo(sDate) >= 0 && curDate.compareTo(eDate) <= 0;
                }).count();
                tempList.add(i);
                tempList.add(j);
                tempList.add(res.intValue());
                dataList.add(tempList);
            }
        }

        return R.ok().put("carList", carList).put("dataList", dataList).put("dateList", dateStrList);
    }
}
